package com.android.tea;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.android.tea.adapter.ContactAdapter;
import com.android.tea.entity.TeaFriend;
import com.android.tea.entity.TeaFriend.FriendType;
import com.android.tea.widght.SideBar;

public class TeaFriendContactsActivity extends Activity {
    protected static final String TAG = "MainActivity";
	private EditText search_edit;
	private ListView mListView;
	private ImageView btn_search;
	private InputMethodManager inputMethodManager;
	private SideBar indexBar;
	private TextView mDialogText;
	private WindowManager mWindowManager;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts);
		inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        initComponents();
    }

	private void initComponents() {
		search_edit = (EditText)findViewById(R.id.edit_search);
		search_edit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				search_edit.setFocusable(true);
				search_edit.setFocusableInTouchMode(true);
				search_edit.requestFocus();
				inputMethodManager.showSoftInput(search_edit, 0);
				
			}
		});
		
		btn_search = (ImageView)findViewById(R.id.btn_search);
		btn_search.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.i(TAG, "-----------");
				inputMethodManager.hideSoftInputFromWindow(search_edit.getWindowToken(), 0);
			}
		});
		mListView = (ListView)findViewById(R.id.listView1);
		mListView.setCacheColorHint(Color.TRANSPARENT);
		Bitmap bitmap = Bitmap.createBitmap(new int[] { 0xFFDEDEDE, 0xFFDEDEDE, 0xFFDEDEDE, 0xFFDEDEDE, 0xFFFFFFFF,
				0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF }, 2, 4, Config.RGB_565);
		mListView.setDivider(new BitmapDrawable(bitmap));
		mListView.setDividerHeight(2);

		ContactAdapter adapter = new ContactAdapter(this, loadData());
		mListView.setAdapter(adapter);
		
		indexBar = (SideBar) findViewById(R.id.sideBar);  
        indexBar.setListView(mListView); 
	}
	
	private void addDialogText() {
		mDialogText = (TextView) LayoutInflater.from(this).inflate(R.layout.list_position, null);
        mDialogText.setVisibility(View.INVISIBLE);
		 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
	                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
	                WindowManager.LayoutParams.TYPE_APPLICATION,
	                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
	                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
	                PixelFormat.TRANSLUCENT);
	        mWindowManager = (WindowManager)this.getParent().getSystemService(Context.WINDOW_SERVICE);
	        mWindowManager.addView(mDialogText, lp);
	        indexBar.setTextView(mDialogText);
	}
	
	@Override
	protected void onResume() {
		addDialogText();
		super.onResume();
	}
	
	@Override
	protected void onStop() {
		Log.i(TAG, "onstop is running");
		mWindowManager.removeView(mDialogText);
		super.onStop();
	}

	private List<TeaFriend> loadData() {
		
		
		
		List<TeaFriend> list = new ArrayList<TeaFriend>();
		TeaFriend member4 = new TeaFriend();
//		member.setAvatar(avatar);
		member4.setTitle("a");
		member4.setType(FriendType.Star_Friend);
		member4.setDesc("一起去shopping?");
		member4.setTimeStamp(System.currentTimeMillis());
		list.add(member4);
		
		TeaFriend member5 = new TeaFriend();
//		member.setAvatar(avatar);
		member5.setTitle("我自己");
		member5.setType(FriendType.Common_Friend);
		member5.setDesc("一起来shopping?");
		member5.setTimeStamp(System.currentTimeMillis());
		list.add(member5);
		
		member5 = new TeaFriend();
//		member.setAvatar(avatar);
		member5.setTitle("天上人间");
		member5.setType(FriendType.Common_Friend);
		member5.setDesc("一起来shopping?");
		member5.setTimeStamp(System.currentTimeMillis());
		list.add(member5);
		
		TeaFriend member = new TeaFriend();
//		member.setAvatar(avatar);
		member.setTitle("爱相随");
		member.setDesc("谢谢你的反馈");
		member.setType(FriendType.Common_Friend);
		member.setTimeStamp(System.currentTimeMillis());
		list.add(member);
		
		member = new TeaFriend();
//		member.setAvatar(avatar);
		member.setTitle("爱相随");
		member.setDesc("谢谢你的反馈");
		member.setType(FriendType.Common_Friend);
		member.setTimeStamp(System.currentTimeMillis());
		list.add(member);
		
//		member = new TeaFriend();
////		member.setAvatar(avatar);
//		member.setTitle("爱相随");
//		member.setDesc("谢谢你的反馈");
//		member.setType(FriendType.Common_Friend);
//		member.setTimeStamp(System.currentTimeMillis());
//		list.add(member);
//		
//		
//		member = new TeaFriend();
////		member.setAvatar(avatar);
//		member.setTitle("爱相随");
//		member.setDesc("谢谢你的反馈");
//		member.setType(FriendType.Common_Friend);
//		member.setTimeStamp(System.currentTimeMillis());
//		list.add(member);
//		
//		member = new TeaFriend();
////		member.setAvatar(avatar);
//		member.setTitle("爱相随");
//		member.setDesc("谢谢你的反馈");
//		member.setType(FriendType.Common_Friend);
//		member.setTimeStamp(System.currentTimeMillis());
//		list.add(member);
//		
//		member = new TeaFriend();
////		member.setAvatar(avatar);
//		member.setTitle("爱相随");
//		member.setDesc("谢谢你的反馈");
//		member.setType(FriendType.Common_Friend);
//		member.setTimeStamp(System.currentTimeMillis());
//		list.add(member);
//		
//		member = new TeaFriend();
////		member.setAvatar(avatar);
//		member.setTitle("爱相随");
//		member.setDesc("谢谢你的反馈");
//		member.setType(FriendType.Common_Friend);
//		member.setTimeStamp(System.currentTimeMillis());
//		list.add(member);
		
		member5 = new TeaFriend();
//		member.setAvatar(avatar);
		member5.setTitle("天上人间");
		member5.setType(FriendType.Common_Friend);
		member5.setDesc("一起来shopping?");
		member5.setTimeStamp(System.currentTimeMillis());
		list.add(member5);
		
		return list;
	}

	
}